package com.oa.ylms.apps.business.plan.impl;

import java.util.List;

import com.oa.ylms.apps.business.organization.IPostInfoService;
import com.oa.ylms.apps.business.plan.ICheckPlanCategoryService;
import com.oa.ylms.apps.business.plan.ICheckPlanService;
import com.oa.ylms.apps.constants.IDEDStates;
import com.oa.ylms.apps.constants.IOrderStates;
import com.oa.ylms.apps.dao.plan.ICheckPlanDao;
import com.oa.ylms.apps.entity.plan.CheckPlanCategoryValue;
import com.oa.ylms.apps.entity.plan.CheckPlanValue;

public class CheckPlanServiceImpl implements ICheckPlanService {
	private ICheckPlanDao planDao;
	private CheckPlanValue checkPlan;
	private ICheckPlanCategoryService checkPlanCategoryService;
	private IPostInfoService postInfoService;

	@Override
	public int addCheckPlan(int categoryId, int postId, String planOrders)
			throws Exception {
		checkPlan.setCheckPlanCategory(checkPlanCategoryService.queryByCategoryId(categoryId));
		checkPlan.setPostInfo(postInfoService.queryByPostId(postId));
		if(checkPlan.getCheckPlanCategory() != null && checkPlan.getPostInfo() != null){
			checkPlan.setPlanOrders(planOrders);
			checkPlan.setStates(IDEDStates.ENABLE);
			return planDao.addCheckPlan(checkPlan);
		}
		return 0;
	}

	@Override
	public int countTPage(Integer categoryId, Integer postId, Integer rows)
			throws Exception {
		if(categoryId!=null){
		checkPlan.setCheckPlanCategory(checkPlanCategoryService.queryByCategoryId(categoryId));
		}
		if(postId!=null){
		checkPlan.setPostInfo(postInfoService.queryByPostId(postId));
		}
		return planDao.countTPage(checkPlan, rows);
	}

	@Override
	public boolean deleteCheckPlan(int planId) throws Exception {
		checkPlan = queryByPlanId(planId);
		if(checkPlan != null){
			checkPlan.setStates(IDEDStates.DELETE);
			return planDao.editCheckPlan(checkPlan);
		}
		return false;
	}

	@Override
	public boolean modifyCheckPlan(int planId, String planOrders)
			throws Exception {
		checkPlan = queryByPlanId(planId);
		if(checkPlan != null){
			checkPlan.setPlanOrders(planOrders);
			return planDao.editCheckPlan(checkPlan);
		}
		return false;
	}

	@Override
	public CheckPlanValue queryByPlanId(int planId) throws Exception {
		checkPlan.setPlanId(planId);
		return planDao.queryByPlanId(checkPlan);
	}

	@Override
	public List<CheckPlanValue> queryCheckPlanWithPage(Integer categoryId, Integer postId,
			Integer rows, Integer pageNo) throws Exception {
		if(categoryId!=null){
		checkPlan.setCheckPlanCategory(checkPlanCategoryService.queryByCategoryId(categoryId));
		}
		if(postId!=null){
		checkPlan.setPostInfo(postInfoService.queryByPostId(postId));
		}
		return planDao.queryCheckPlanWithPage(checkPlan, rows, pageNo);
	}
	
	@Override
	public CheckPlanValue queryByPostIdAndCategoryId(int categoryId, int postId)
			throws Exception {
		checkPlan.setCheckPlanCategory(checkPlanCategoryService.queryByCategoryId(categoryId));
		checkPlan.setPostInfo(postInfoService.queryByPostId(postId));
		if(checkPlan.getCheckPlanCategory() != null && checkPlan.getPostInfo() != null){
			for(CheckPlanValue cpv : planDao.queryCheckPlanWithPage(checkPlan, 1, 1)){
				return cpv;
			}
		}
		return null;
	}
	
	public int getTotalNumber(){
		return planDao.getTotalNumber();
	}
	
	public ICheckPlanDao getPlanDao() {
		return planDao;
	}

	public void setPlanDao(ICheckPlanDao planDao) {
		this.planDao = planDao;
	}

	public CheckPlanValue getCheckPlan() {
		return checkPlan;
	}

	public void setCheckPlan(CheckPlanValue checkPlan) {
		this.checkPlan = checkPlan;
	}

	public ICheckPlanCategoryService getCheckPlanCategoryService() {
		return checkPlanCategoryService;
	}

	public void setCheckPlanCategoryService(
			ICheckPlanCategoryService checkPlanCategoryService) {
		this.checkPlanCategoryService = checkPlanCategoryService;
	}

	public IPostInfoService getPostInfoService() {
		return postInfoService;
	}

	public void setPostInfoService(IPostInfoService postInfoService) {
		this.postInfoService = postInfoService;
	}
	/**
	 * 一个公共方法
	 */
	public int getStates(String planOrders,int currentState){
		String[] deptNo = planOrders.split(",");
		for(int i=0;i<deptNo.length;i++){
			int index = Integer.parseInt(deptNo[i]);
			if(currentState==index&&i!=deptNo.length-1){
				return Integer.parseInt(deptNo[i+1]);
			}
			if (i == deptNo.length - 1) {
				return IOrderStates.PASSED;
			}
		}
		return 0;
	}
}
